﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Workflow.Node;
using System.Data;

using Jzy.Workflow.Flow;
using Frame.Lib.Utilities;

namespace Jzy.Web.WorkFlow.Design
{
    public partial class RightSet : System.Web.UI.Page
    {
        private string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }

        private string ControlName
        {
            get
            {
                return Request["Name"];
            }
        }

        private FlowNodeBO _node = new FlowNodeBO();
        private ControlRightBO _right = new ControlRightBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
                lblName.Text = ControlName;
            }
        }

        private void BindView()
        {
            DataTable dt =  _node.GetDataTable(int.Parse(FlowID), this.ControlName);
            DataView dv = dt.DefaultView;
            dv.RowFilter = "TypeID <> " + (int)FlowNodeType.结束 + " and TypeID <> " + (int)FlowNodeType.拟稿;
            rpt.DataSource = dv;
            rpt.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            foreach (RepeaterItem item in rpt.Items)
            {
                HiddenField hidNodeID = (HiddenField)item.FindControl("hidID");

                TextBox txtEdit = (TextBox)item.FindControl("txtEdit");
                HiddenField hidEdit = (HiddenField)item.FindControl("hidEdit");

                ControlRightInfo info = new ControlRightInfo();
                info.ControlName = ControlName;
                info.FlowID = int.Parse(FlowID);
                info.NodeID = int.Parse(hidNodeID.Value);
                info.RightID = (int)RightName.可编辑;
                _right.Delete(info.ControlName, info.NodeID, info.RightID);
                List<int> userIDs = StrUtil.SplitToInt(hidEdit.Value);               
                foreach (int userID in userIDs)
                {
                    info.ID = 0;
                    info.UserID = userID;
                    _right.Save(info);
                }
               
                TextBox txtBrowse = (TextBox)item.FindControl("txtBrowse");
                HiddenField hidBrowse = (HiddenField)item.FindControl("hidBrowse");
                info.RightID = (int)RightName.不可见;
                _right.Delete(info.ControlName, info.NodeID, info.RightID);
                userIDs = StrUtil.SplitToInt(hidBrowse.Value);               
                foreach (int userID in userIDs)
                {
                    info.ID = 0;
                    info.UserID = userID;
                    _right.Save(info);
                }
            }

            JSUtil.CloseDialog("保存成功");
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                TextBox txtEdit = (TextBox)e.Item.FindControl("txtEdit");
                HiddenField hidEdit = (HiddenField)e.Item.FindControl("hidEdit");

                TextBox txtBrowse = (TextBox)e.Item.FindControl("txtBrowse");
                HiddenField hidBrowse = (HiddenField)e.Item.FindControl("hidBrowse");

                HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

                string url = "SelectUser.aspx?NodeID=" + hidID.Value;
                JSUtil.ShowDlg(hidEdit, txtEdit, url);
                JSUtil.ShowDlg(hidBrowse, txtBrowse, url);
            }
        }
    }
}
